3 use Psr\Log\NullLogger
;
4 use Wikimedia\Rdbms\Blob
;
5 use Wikimedia\Rdbms\Database
;
6 use Wikimedia\Rdbms\DatabaseSqlite
;
7 use Wikimedia\Rdbms\ResultWrapper
;
8 use Wikimedia\Rdbms\TransactionProfiler
;
9 use Wikimedia\TestingAccessWrapper
;
16 class DatabaseSqliteTest
extends \MediaWikiIntegrationTestCase
{
17 /** @var DatabaseSqlite */
20 protected function setUp() {
23 if ( !Sqlite
::isPresent() ) {
24 $this->markTestSkipped( 'No SQLite support detected' );
26 $this->db
= $this->getMockBuilder( DatabaseSqlite
::class )
27 ->setConstructorArgs( [ [
28 'dbFilePath' => ':memory:',
35 'agent' => 'unit-tests',
36 'flags' => DBO_DEFAULT
,
39 'trxProfiler' => new TransactionProfiler(),
40 'connLogger' => new NullLogger(),
41 'queryLogger' => new NullLogger(),
42 'errorLogger' => null,
43 'deprecationLogger' => null,
44 ] ] )->setMethods( [ 'query' ] )
46 $this->db
->initConnection();
47 $this->db
->method( 'query' )->willReturn( true );
48 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
49 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
55 * @return string|string[]|null
57 private function replaceVars( $sql ) {
58 $wrapper = TestingAccessWrapper
::newFromObject( $this->db
);
59 // normalize spacing to hide implementation details
60 return preg_replace( '/\s+/', ' ', $wrapper->replaceVars( $sql ) );
63 private function assertResultIs( $expected, $res ) {
64 $this->assertNotNull( $res );
66 foreach ( $res as $row ) {
67 foreach ( $expected[$i] as $key => $value ) {
68 $this->assertTrue( isset( $row->$key ) );
69 $this->assertEquals( $value, $row->$key );
73 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
76 public static function provideAddQuotes() {
82 'foo bar', "'foo bar'"
84 [ // #2: including quote
85 'foo\'bar', "'foo''bar'"
87 // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
92 [ // #4: blob object (must be represented as hex)
104 * @dataProvider provideAddQuotes()
105 * @covers DatabaseSqlite::addQuotes
107 public function testAddQuotes( $value, $expected ) {
109 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
110 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
112 // ok, quoting works as expected, now try a round trip.
113 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
115 $this->assertTrue( $re !== false, 'query failed' );
117 $row = $re->fetchRow();
119 if ( $value instanceof Blob
) {
120 $value = $value->fetch();
123 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
125 $this->fail( 'query returned no result' );
130 * @covers DatabaseSqlite::replaceVars
132 public function testReplaceVars() {
133 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
136 "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
137 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
139 "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, "
140 . "foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', "
141 . "foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;"
146 "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
148 "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
152 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
153 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
156 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
157 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
161 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
162 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
164 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
165 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
168 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
169 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
172 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
173 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
176 $this->assertEquals( "DROP INDEX foo",
177 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
180 $this->assertEquals( "DROP INDEX foo -- dropping index",
181 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
183 $this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
184 $this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
189 * @covers DatabaseSqlite::tableName
191 public function testTableName() {
193 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
194 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
195 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
196 $db->tablePrefix( 'foo_' );
197 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
198 $this->assertEquals( 'foo_bar', $db->tableName( 'bar' ) );
202 * @covers DatabaseSqlite::duplicateTableStructure
204 public function testDuplicateTableStructure() {
205 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
206 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
207 $db->query( 'CREATE INDEX index1 ON foo(foo)' );
208 $db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' );
210 $db->duplicateTableStructure( 'foo', 'bar' );
211 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
212 $db->selectField( 'sqlite_master', 'sql', [ 'name' => 'bar' ] ),
213 'Normal table duplication'
215 $indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' );
216 $index = $indexList->next();
217 $this->assertEquals( 'bar_index1', $index->name
);
218 $this->assertEquals( '0', $index->unique
);
219 $index = $indexList->next();
220 $this->assertEquals( 'bar_index2', $index->name
);
221 $this->assertEquals( '1', $index->unique
);
223 $db->duplicateTableStructure( 'foo', 'baz', true );
224 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
225 $db->selectField( 'sqlite_temp_master', 'sql', [ 'name' => 'baz' ] ),
226 'Creation of temporary duplicate'
228 $indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' );
229 $index = $indexList->next();
230 $this->assertEquals( 'baz_index1', $index->name
);
231 $this->assertEquals( '0', $index->unique
);
232 $index = $indexList->next();
233 $this->assertEquals( 'baz_index2', $index->name
);
234 $this->assertEquals( '1', $index->unique
);
235 $this->assertEquals( 0,
236 $db->selectField( 'sqlite_master', 'COUNT(*)', [ 'name' => 'baz' ] ),
237 'Create a temporary duplicate only'
242 * @covers DatabaseSqlite::duplicateTableStructure
244 public function testDuplicateTableStructureVirtual() {
245 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
246 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
247 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
249 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
251 $db->duplicateTableStructure( 'foo', 'bar' );
252 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
253 $db->selectField( 'sqlite_master', 'sql', [ 'name' => 'bar' ] ),
254 'Duplication of virtual tables'
257 $db->duplicateTableStructure( 'foo', 'baz', true );
258 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
259 $db->selectField( 'sqlite_master', 'sql', [ 'name' => 'baz' ] ),
260 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
265 * @covers DatabaseSqlite::deleteJoin
267 public function testDeleteJoin() {
268 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
269 $db->query( 'CREATE TABLE a (a_1)', __METHOD__
);
270 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__
);
279 [ 'b_1' => 2, 'b_2' => 'a' ],
280 [ 'b_1' => 3, 'b_2' => 'b' ],
284 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', [ 'b_2' => 'a' ], __METHOD__
);
285 $res = $db->query( "SELECT * FROM a", __METHOD__
);
286 $this->assertResultIs( [
297 public function testEntireSchema() {
300 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
301 if ( $result !== true ) {
302 $this->fail( $result );
304 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
308 * Runs upgrades of older databases and compares results with current schema
309 * @todo Currently only checks list of tables
312 public function testUpgrades() {
313 global $IP, $wgVersion, $wgProfiler;
317 // '1.13', disabled for now, was totally screwed up
318 // SQLite wasn't included in 1.14
330 // Mismatches for these columns we can safely ignore
332 'user_newtalk.user_last_timestamp', // r84185
335 $currentDB = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
336 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
338 $profileToDb = false;
339 if ( isset( $wgProfiler['output'] ) ) {
340 $out = $wgProfiler['output'];
341 if ( $out === 'db' ) {
343 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
348 if ( $profileToDb ) {
349 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
351 $currentTables = $this->getTables( $currentDB );
352 sort( $currentTables );
354 foreach ( $versions as $version ) {
355 $versions = "upgrading from $version to $wgVersion";
356 $db = $this->prepareTestDB( $version );
357 $tables = $this->getTables( $db );
358 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
359 foreach ( $tables as $table ) {
360 $currentCols = $this->getColumns( $currentDB, $table );
361 $cols = $this->getColumns( $db, $table );
363 array_keys( $currentCols ),
365 "Mismatching columns for table \"$table\" $versions"
367 foreach ( $currentCols as $name => $column ) {
368 $fullName = "$table.$name";
371 (bool)$cols[$name]->pk
,
372 "PRIMARY KEY status does not match for column $fullName $versions"
374 if ( !in_array( $fullName, $ignoredColumns ) ) {
376 (bool)$column->notnull
,
377 (bool)$cols[$name]->notnull
,
378 "NOT NULL status does not match for column $fullName $versions"
382 $cols[$name]->dflt_value
,
383 "Default values does not match for column $fullName $versions"
387 $currentIndexes = $this->getIndexes( $currentDB, $table );
388 $indexes = $this->getIndexes( $db, $table );
390 array_keys( $currentIndexes ),
391 array_keys( $indexes ),
392 "mismatching indexes for table \"$table\" $versions"
400 * @covers DatabaseSqlite::insertId
402 public function testInsertIdType() {
403 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
405 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
406 $this->assertInstanceOf( ResultWrapper
::class, $databaseCreation, "Database creation" );
408 $insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__
);
409 $this->assertTrue( $insertion, "Insertion worked" );
411 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
412 $this->assertTrue( $db->close(), "closing database" );
416 * @covers DatabaseSqlite::insert
418 public function testInsertAffectedRows() {
419 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
420 $db->query( 'CREATE TABLE testInsertAffectedRows ( foo )', __METHOD__
);
422 $insertion = $db->insert(
423 'testInsertAffectedRows',
431 $this->assertTrue( $insertion, "Insertion worked" );
433 $this->assertSame( 3, $db->affectedRows() );
434 $this->assertTrue( $db->close(), "closing database" );
437 private function prepareTestDB( $version ) {
438 static $maint = null;
439 if ( $maint === null ) {
440 $maint = new FakeMaintenance();
441 $maint->loadParamsAndArgs( null, [ 'quiet' => 1 ] );
445 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
446 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
447 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
448 $updater->doUpdates( [ 'core' ] );
453 private function getTables( $db ) {
454 $list = array_flip( $db->listTables() );
456 'external_user', // removed from core in 1.22
457 'math', // moved out of core in 1.18
458 'trackbacks', // removed from core in 1.19
460 'searchindex_content',
461 'searchindex_segments',
462 'searchindex_segdir',
464 'searchindex_docsize',
467 foreach ( $excluded as $t ) {
470 $list = array_flip( $list );
476 private function getColumns( $db, $table ) {
478 $res = $db->query( "PRAGMA table_info($table)" );
479 $this->assertNotNull( $res );
480 foreach ( $res as $col ) {
481 $cols[$col->name
] = $col;
488 private function getIndexes( $db, $table ) {
490 $res = $db->query( "PRAGMA index_list($table)" );
491 $this->assertNotNull( $res );
492 foreach ( $res as $index ) {
493 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
494 $this->assertNotNull( $res2 );
495 $index->columns
= [];
496 foreach ( $res2 as $col ) {
497 $index->columns
[] = $col;
499 $indexes[$index->name
] = $index;
509 public function testCaseInsensitiveLike() {
510 // TODO: Test this for all databases
511 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
512 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
513 $row = $res->fetchRow();
514 $this->assertFalse( (bool)$row['a'] );
518 * @covers DatabaseSqlite::numFields
520 public function testNumFields() {
521 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
523 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
524 $this->assertInstanceOf( ResultWrapper
::class, $databaseCreation, "Failed to create table a" );
525 $res = $db->select( 'a', '*' );
526 $this->assertEquals( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
527 $insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__
);
528 $this->assertTrue( $insertion, "Insertion failed" );
529 $res = $db->select( 'a', '*' );
530 $this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
532 $this->assertTrue( $db->close(), "closing database" );
536 * @covers \Wikimedia\Rdbms\DatabaseSqlite::__toString
538 public function testToString() {
539 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
541 $toString = (string)$db;
543 $this->assertContains( 'sqlite object', $toString );
547 * @covers \Wikimedia\Rdbms\DatabaseSqlite::getAttributes()
549 public function testsAttributes() {
550 $attributes = Database
::attributesFromType( 'sqlite' );
551 $this->assertTrue( $attributes[Database
::ATTR_DB_LEVEL_LOCKING
] );